/**
 * Created by Administrator on 2017/3/22 0022.
 */
var Page6={
    create:function()
    {
        var page={};
        var bool_down;
        var start0={x:0,y:0};
        var offeset={x:0,y:0};
        var down={x:0,y:0};

        page.ui=new PIXI.DisplayObjectContainer();

        var bg=new PIXI.Sprite(resource['assets/images/p6_bg.png'].texture);
        bg.x=0;
        bg.y=0;
        page.ui.addChild(bg);

        var bar=new PIXI.Graphics();
        bar.beginFill(0x0,1);
        bar.drawRect(0,0,1136,80);
        bar.endFill();
        bar.y=535;
        page.ui.addChild(bar);

        var hand=new PIXI.Sprite(resource['assets/images/p6_hand.png'].texture);
        //hand.anchor.set(0.5);
        hand.x=267;
        hand.y=-31;
        page.ui.addChild(hand);
        hand.on('pointerdown',downHandler);
        hand.on('pointermove',moveHandler);
        hand.on('pointerup',upHandler);

        var hat=new PIXI.Sprite(resource['assets/images/p6_curtain.png'].texture);
        offeset.x=46;
        offeset.y=13;
        hat.x=hand.x+offeset.x;
        hat.y=hand.y+offeset.y;
        page.ui.addChild(hat);

        var cover=new PIXI.Sprite(resource['assets/images/p6_cover.png'].texture);
        cover.x=0;
        cover.y=0;
        page.ui.addChild(cover);

        page.ui.addChild(hand);

        var t0=new PIXI.Sprite(resource['assets/images/p6_t0.png'].texture);
        t0.anchor.set(0.5);
        t0.x=568;
        t0.y=564;
        page.ui.addChild(t0);

        var tip=new PIXI.Sprite(resource['assets/images/p6_tip.png'].texture);
        tip.x=0;
        tip.y=0;
        page.ui.addChild(tip);
        TweenMax.to(tip,1,{alpha:0.8,x:-30,yoyo:true,repeat:-1,ease:Cubic.easeInOut});





        page.In=function()
        {
            bg.alpha=0;
            bg.x=0;
            TweenMax.to(bg,1,{x:0,alpha:1});

            hat.alpha=0;
            TweenMax.to(hat,1,{delay:0.0,alpha:1});

            cover.alpha=0;
            TweenMax.to(cover,1,{delay:0.0,alpha:1});

            bar.y=535+100;
            bar.alpha=1;
            TweenMax.to(bar,1,{delay:1.5,y:535,alpha:1,ease:Cubic.easeInOut});

            t0.alpha=0;
            TweenMax.to(t0,1,{delay:1,alpha:1});

            // hat.scale.x=hat.scale.y=0;
            // TweenMax.to(hat.scale,1,{delay:2.3,x:1,y:1,ease:Back.easeOut});

            hand.x=202;
            hand.y=-87;
            hand.alpha=0
            TweenMax.to(hand,1.5,{delay:2.5,x:261,y:-13,alpha:1,ease:Back.easeOut,onComplete:function()
            {
                hand.interactive=true;
            }});

        }
        function downHandler(e)
        {
            bool_down=true;
            start0.x=hand.x;
            start0.y=hand.y;
            // start1.x=hat.x;
            // start1.y=hat.y;
            down.x=e.data.global.x;
            down.y= e.data.global.y;
        }
        function moveHandler(e)
        {
            if(bool_down)
            {
                hand.x=start0.x+(e.data.global.x-down.x);
                // hand.y=start0.y+(e.data.global.y-down.y);
                hat.x=hand.x+offeset.x;
                // hat.y=hand.y+offeset.y
                // if(hand.y<0)
                // {
                //     hand.y=0;
                //     hat.y=hand.y+offeset.y
                // }
                // else if(hand.y>367)
                // {
                //     hand.y=367;
                //     hat.y=hand.y+offeset.y
                // }
                if(hand.x<-13)
                {
                    hand.x=-13;
                    hat.x=hand.x+offeset.x;
                }
                else if(hand.x>267)
                {
                    hand.x=267;
                    hat.x=hand.x+offeset.x;
                }
                console.log((hat.x-tip.x)*(hat.x-tip.x)+(hat.y-tip.y)*(hat.y-tip.y))
                if(hat.x<=160)
                {
                    bool_down=false;
                    hand.interactive=false;
                    TweenMax.killTweensOf(tip);
                    TweenMax.to(tip,1,{alpha:0});

                    TweenMax.to(hat,1.5,{x:-13+offeset.x});
                    TweenMax.to(hand,1,{alpha:0});

                    TweenMax.to(bar,1,{delay:0,y:535+100,alpha:1,ease:Cubic.easeInOut});
                    TweenMax.to(t0,1,{delay:1,alpha:0});

                    setTimeout(function () {
                        var arr=[1,4]
                        var event = new CustomEvent("Game_PageIn",{'detail':arr});
                        document.dispatchEvent(event);
                    },2000);
                }

            }
            speed=(461-hand.y)/20000;
        }
        function upHandler(e)
        {
            bool_down=false;
        }


        return page;
    }

}